<?php

require_once 'inc/common.php';
$data = array();
$name = $db->q1('
	SELECT name
	FROM users
	WHERE fbid = ?
	AND fbid IN (
		SELECT friend_fbid
		FROM friends
		WHERE user_fbid = ?
	)
', $_GET['fbid'], $_SESSION['me']['fbid']);
if ($name) {
	//$fbposts = $fb->api("/$_GET[fbid]/posts?limit=5000");
    
$stmt = $db->prepare('SELECT content FROM posts WHERE user_fbid=?');
$stmt->execute(array($_GET['fbid']));
$resultset = $stmt->fetchAll();
$postcount = count($resultset);
if($resultset){
//TODO:
//Start Shouting
$totalCharCount = 0;
$upperCaseCount = 0;
$upperCasePattern = "/[A-Z]/";
foreach($resultset as $r)
{
   $chars = str_split($r['content']);
   
   foreach($chars as $c)
   {
       $upperCaseCount+=preg_match_all($upperCasePattern,$c,$matches);
       $totalCharCount++;
   }
}
$shoutinggraph = number_format($upperCaseCount/$totalCharCount*100,2);
$shoutingPercentage = $shoutinggraph.'% of all posts';
//End Shouting
//
//Start Happy 
//
//
//Start Cursing
$cursewords = array("fuck","suck","hate","stupid","fking");

$cursingcount = 0;
foreach($resultset as $cp)
{
   $filteredpost = str_replace($cursewords, "[CURSEWORDS]", $cp['content']);
   if(strpos($filteredpost, "[CURSEWORDS]")!==false)
   {
     $cursingcount++;   
   }
}
$cursinggraph =number_format($cursingcount/$postcount*100,2);
$cursingcountpercent = $cursinggraph.'% of all posts';
//

//End Cursing
//
//Start Talkative post per week
$stmt = $db->prepare('SELECT UNIX_TIMESTAMP(MIN(dt)) AS t FROM posts WHERE user_fbid=?');
$stmt->execute(array($_GET['fbid']));
$resulttime=$stmt->fetch();
$weekscount = (time()- $resulttime['t'])/86400/7;

$talkativegraph = number_format($postcount/$weekscount,2);
$talkativePostPerDay = $talkativegraph.' posts per week';

    
 //End Talkative

//Start Hesitant
//define pattern as '/\.{1,}/' for testing purpose
//else, ellipses(...)({3,})
//if hit then make it to one post
$hesitantpattern = '/\.{3,}/';
$totalhesitantcount = 0;
foreach($resultset as $h)
{
    //$wordsp = explode(" ",$h['content']);
    //$totalhesitantcount+=preg_match_all($hesitantpattern,$wc,$matches);
    if(strpos($h['content'], "...")!==false)
    {
        $totalhesitantcount++;
    }   
    
}

$hesitantgraph = number_format($totalhesitantcount/$postcount*100,2);
$hesitantpercent = $hesitantgraph.'% of all posts';

//End Hesitant
//Grammar Nazi
$grammarnazipattern = '/[:;()\'\"\.\-\!\^\$\+\*\|]/';
$punctuationcount =0;
foreach($resultset as $g)
{
        //number of punctuation characters
        $punctuationcount += preg_match_all($grammarnazipattern,$g['content'],$matches);
}
$grammarnazigraph = number_format($punctuationcount/$totalCharCount*100,2);
$grammarnazipercentage = $grammarnazigraph.'% of all posts';

$friends = $db->q('
	SELECT fbid, name
	FROM users
	WHERE fbid
	IN (
		SELECT friend_fbid fbid
		FROM friends
		WHERE user_fbid = ?
	)
	ORDER BY name
', $_SESSION['me']['fbid']);
  
	foreach ($resultset as $r) {
		if (empty($r['message'])) continue;
		$r['content'] = $r['message'];
		$cp = $r;
		$h = $r;
		$g = $r;
		
		$chars = str_split($r['content']);
		foreach ($chars as $c) {
			$upperCaseCount+=preg_match_all($upperCasePattern, $c, $matches);
			$totalCharCount++;
		}
		$filteredpost = str_replace($cursewords, "[CURSEWORDS]", $cp['content']);
		if (strpos($filteredpost, "[CURSEWORDS]") !== false) {
			$cursingcount++;
		}
		if(strpos($h['content'], "...")!==false)
		{
			$totalhesitantcount++;
		}
		$punctuationcount += preg_match_all($grammarnazipattern,$g['content'],$matches);
	}
	
	$shoutinggraph = number_format($upperCaseCount / $totalCharCount * 100, 2);
	$shoutingPercentage = $shoutinggraph . '% of all posts';
	$cursinggraph = number_format($cursingcount / $postcount * 100, 2);
	$cursingcountpercent = $cursinggraph . '% of all posts';
	$stmt = $db->prepare('SELECT UNIX_TIMESTAMP(MIN(dt)) AS t FROM posts WHERE user_fbid=?');
	$stmt->execute(array($_GET['fbid']));
	$resulttime = $stmt->fetch();
	$weekscount = (time() - $resulttime['t']) / 86400 / 7;
	$talkativegraph = number_format($postcount / $weekscount, 2);
	$talkativePostPerDay = $talkativegraph . ' posts per week';
	$hesitantgraph = number_format($totalhesitantcount/$postcount*100,2);
	$hesitantpercent = $hesitantgraph.'% of all posts';
	$grammarnazigraph = number_format($punctuationcount/$totalCharCount*100,2);
	$grammarnazipercentage = $grammarnazigraph.'% of all posts';

	$data['Shouting'] = (float)$shoutinggraph;
	$data['Cursing'] = (float)$cursinggraph;
	$data['Talkative'] = (float)$talkativegraph;
	$data['Hesitant'] = (float)$hesitantgraph;
	$data['Grammar Nazi'] = (float)$grammarnazigraph;
}
}
header('Content-Type: application/json');
echo json_encode($data);
?>